<?php
namespace backend\model;
use common\model\common_model;
/**
 * admin_auth模型数据库操作类
 */
 
class admin_access extends common_model {
	public $table_name = 'admin_access';
	public $category = '';
	public $_pk  = 'access_id';
	public function __construct() 
    {
		$this->db_config = load_config('database');
		$this->db_setting = 'default';
		parent::__construct();
	    
	}

	public function add($param)
    {
        if(empty($param['access_name']))
        {
            throw new \Exception('名称不能为空',-100);
        }
        if(empty($param['access_rule']))
        {
            throw new \Exception('规则不能为空',-100);
        }
        //最多只支持4级操作
        if($param['parent_id'] > 0){
            $access_list = $this->getTreeData(0,0,true);
            foreach($access_list AS &$accessValue){
                if($accessValue['id'] == $param['parent_id'] && $accessValue['level'] >=3){
                    throw new \Exception('最多只支持4级操作',-102);
                }
            }
        }
        $_data = [
            'access_name'=>$param['access_name'],
            'parent_id'  =>$param['parent_id'],
            'is_show'    =>$param['is_show'],
            'access_rule'=>$param['access_rule'],
            'update_time'=>time(),
        ];
        $ret = $this->insert($_data);
        if(!$ret){
            throw new \Exception('保存数据失败'.$this->getError(),-105);
        }
        return true;
    }

    public function edit($param)
    {
        if(empty($param['access_name']))
        {
            throw new \Exception('名称不能为空',-100);
        }
        if(empty($param['access_rule']))
        {
            throw new \Exception('规则不能为空',-110);
        }
        if(empty($param['access_id']))
        {
            throw new \Exception('access_id不能为空',-120);
        }
        $_data = [
            'access_name'=>$param['access_name'],
            'parent_id'  =>$param['parent_id'],
            'is_show'    =>$param['is_show'],
            'access_rule'=>$param['access_rule'],
            'update_time'=>time(),
        ];
        $ret = $this->update($_data,['access_id'=>$param['access_id']]);
        if(!$ret){
            throw new \Exception('保存数据失败'.$this->getError(),-150);
        }
        return true;
    }

    public function del($id)
    {
        if(empty($id))
        {
            throw new \Exception('id不能为空',-104);
        }
        $item = $this->findOne($id);
        if(!$item){
            throw new \Exception('记录不存在',-106);
        }
        $parent = $this->get_one(['parent_id'=>$id]);
        if($parent){
            throw new \Exception('有子分类,无法删除',-106);
        }

        $ret = $this->delete([$this->_pk=>$id]);
        if(!$ret){
            throw new \Exception('删除数据失败'.$this->getError(),-105);
        }
        return true;
    }


    public function getTreeData($root_id=0,$select_id=0,$is_return_array=false,$isShowAll=true)
    {
        $list = $this->select();
        load_class('common_tree');
        $tree = new \common_tree();
        $tree->statusField= 'is_show';
        $_list = [];
        if($list){
            foreach($list as &$item){
                $tree->setNode($item['access_id'],
                    $item['parent_id'],
                    $item['access_name'],
                    [
                        'parent_id'=>$item['parent_id'],
                        'is_show'=>$item['is_show'],
                        'access_rule'=>$item['access_rule']
                    ]
                );
            }
            $_list = $tree->getAllData($root_id,$select_id,$is_return_array,0,$isShowAll);
            if($is_return_array){
                foreach($_list AS $k=>&$v){
                    $_list[$k]['children_ids'] = $tree->getChilds($v['id']);
                }
            }
        }
        return $_list;
    }


}
?>